
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ASR-Node</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #app {
        width: 1000px;
        margin: 100px auto 0;
        text-align: center;
      }
      h3 {
        margin-bottom: 20px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h3>ASR-Bot</h3>
      <main>
        <span>发送：</span>
        <span id="send_msg"></span>
        <br />
        <span>接收：</span>
        <span id="rec_msg"></span>
      </main>
    </div>
    <script type="module">
      const speak = (str) => {
        rec_msg.textContent = str;
        const speakText = new SpeechSynthesisUtterance(str); // 转换字符
        speechSynthesis.speak(speakText); // 语音合成
      };
      const listen = () => {
        // 创建语音识别对象
        const recognition = new webkitSpeechRecognition();
        // 语音设置成中文
        recognition.lang = "zh-CN";
        // 当识别到语音时触发事件
        recognition.addEventListener("result", (event) => {
          const { results } = event;
          console.log(results);
          const len = results.length;
          const { transcript } = results[len - 1][0];
          send_msg.textContent = transcript;
          speak(transcript);

          
        });
        return recognition;
      };
 
      // 开始识别
      listen().start();
    </script>
  </body>
</html>